package com.first.controller;

import com.first.config.TestSender;
import com.first.entity.UsersVO;
import com.first.feign.YuDongFeignClient;
import com.first.servers.UsersServers;
import com.first.entity.Users;
import com.common.base.Result;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.kckj.comm.util.ExcelUtil;
import com.kckj.comm.util.KckjLog;
import com.kckj.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/users")
@Api(value = "firsttest")//swagger使用
public class testController {
    @Resource
    private UsersServers servers;

    @Autowired
    private YuDongFeignClient yuDongFeignClient;

    @Resource
    private RedisUtil redisUtil;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Autowired
    private TestSender testSender;

    @GetMapping("/allUsers")
    @KckjLog(moudleName = "用户",functionName = "查询",typeName = "333")
    public List<Users> getAllUsers(){
        return servers.getAllUsers();
    }
    @GetMapping("/usersById")
    @KckjLog(moudleName = "用户",functionName = "查询",typeName = "666")
    public Users getUsersById(@RequestParam("id") String id){
        System.out.println("这是yudong-server的数据");
        return yuDongFeignClient.getUsersById(id);
    }
    @GetMapping("/dingshi")
    //@Scheduled(cron="* * * * * *")
    public String str(){
           return servers.str();
    }

    /**
     * 保存redis数据  启动chaoqinag服务
     * http://localhost:8083/chaoqiang/swagger-ui.html#/
     * @return
     */
    @ApiOperation(value = "保存redis数据",notes = "测试保存redis数据")//swagger使用
    @PostMapping("/saveRedisData")
    public String saveRedisData(){
        return servers.saveRedisData();
    }

    @ApiOperation(value = "调用工具类",notes = "测试调用工具类DateUtils")//swagger使用
    @GetMapping("/findUtilDate")
    public String findUtilDate(){
        System.out.println("dfssdfsdf");
        return servers.findUtilDate();
    }

    //获取redis数据
    @KckjLog(moudleName="moudleName获取redis数据", functionName="functionName获取redis数据", typeName="typeName获取redis数据")
    @ApiOperation(value = "获取redis数据",notes = "获取redis数据")//swagger使用
    @GetMapping("/getRedis")
    @ResponseBody
    public Object getRedis(){
        return redisUtil.get("USER:chaoqiang");
    }

    //导出用户列表
    @ApiOperation(value = "导出用户列表",notes = "导出用户列表")//swagger使用
    @GetMapping("/exportUsers")
    @ResponseBody
    public String exportUsers(HttpServletRequest request, HttpServletResponse response){
        try {
            List<UsersVO> usersVOList = servers.exportUsers();
            ExcelUtil<UsersVO> util =new ExcelUtil<>(UsersVO.class);
            Result result=util.exportExcel(response,usersVOList,"用户列表");
            return "导出成功";
        }catch (Exception e){
            System.out.println(e.getMessage());
            return e.getMessage();
        }
    }

    //导入用户列表
    @ApiOperation(value = "导入用户数据",notes = "导入用户数据")//swagger使用
    @PostMapping("/importUsers")
    @ResponseBody
    public String importUsers(MultipartFile file){
        return servers.importUsers(file);
    }

    //生成二维码服务
    @ApiOperation(value = "二维码生成服务",notes = "二维码生成服务")//swagger使用
    @GetMapping("/creatQCode")
    public String creatQCode() throws IOException, WriterException {
        String uuid = UUID.randomUUID().toString();
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix matrix = writer.encode(uuid, BarcodeFormat.QR_CODE, 320, 320);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(matrix, "PNG", baos);
        MatrixToImageWriter.writeToFile(matrix, "PNG", new File("/Users/fanchaoqiang/qr.png"));
        Base64.Encoder encoder = Base64.getEncoder();
        String text = encoder.encodeToString(baos.toByteArray());
        return text;
    }

    //发送kafka消息
    @ApiOperation(value = "发送kafka消息",notes = "发送kafka消息")//swagger使用
    @PostMapping("/sendkafkaMsg")
    @KckjLog(moudleName = "kafka",functionName = "kafka",typeName = "插入")
    public String sendkafkaMsg(String msg) {
        kafkaTemplate.send("test","name",msg);
        return "send ok";
    }

    //发送RabbitMQ消息
    @ApiOperation(value = "发送RabbitMQ消息",notes = "发送RabbitMQ消息")//swagger使用
    @GetMapping("/sendRabbitMQMsg")
    @KckjLog(moudleName = "RabbitMQ",functionName = "RabbitMQ",typeName = "发送")
    public String sendRabbitMQMsg() {
        testSender.sendMsg();
        return "send ok";
    }

}
